<template>
	<view>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index==TabCur?'text-orange cur':''" v-for="(item,index) in tabList" :key="index" @tap="tabSelect" :data-id="index">
					{{item}}
				</view>
			</view>
		</scroll-view>
		<uni-clientdb ref="udb" v-slot:default="{data, loading, error, options}" :options="formData" :collection="collection"
		 :field="field" @load="load" orderby="_id desc" :where="dbWhere">
			<view class="cu-list menu-avatar">
				<view class="cu-item" v-for="item in data" :key="item._id">
					<view class="cu-avatar round lg bg-brown" :style="'background-image:url('+item.giftInfo.imgPath[0]+');'"></view>
					<view class="content">
						<view class="text-grey">{{item.giftInfo.title}}|{{item.giftInfo.attr[item.giftInfo.AttrIndex]}}</view>
						<view class="text-gray text-sm flex">
							<view class="text-cut">
								{{item.create_date}}
							</view>
						</view>
					</view>
					<view>
						{{item.username}}
					</view>
					<view v-if="!item.send">
						<button size="mini" @click="sendGift(item._id)">发货</button>
					</view>
				</view>
			</view>
		</uni-clientdb>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				collection: 'giftOrders',
				field: '_id, send, wx_openid, create_date, giftInfo, username',
				formData: {
					status: 'loading', // 加载状态
				},
				TabCur: 0,
				tabList: ['待发货', '全部'],
				dbWhere: {
						send: false
					}
			}
		},
		
		methods: {
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				if (this.TabCur == 0) {
					this.dbWhere = {
						send: false
					};
				} else if (this.TabCur == 1) {
					this.dbWhere = '';
				}
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},
			/**
			 * 下拉刷新回调函数
			 */
			onPullDownRefresh() {
				this.tipShow = true
				this.formData.status = 'more'
				this.$refs.udb.loadData({
					clear: true
				}, () => {
					this.tipShow = false
					uni.stopPullDownRefresh()
				})
			},
			/**
			 * 上拉加载回调函数
			 */
			onReachBottom() {
				this.$refs.udb.loadMore()
			},
			
			load(data,ended){
				if(ended){
					this.formData.status = 'noMore'
				}
			},
			
			sendGift: function(id) {
				uniCloud.database().collection('giftOrders')
				.doc(id)
				.update({
					send: true
				})
			}
		}
	}
</script>

<style>

</style>
